package com.nq.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.ImmutableMap;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云泰股数据接口
 */
public class TGStockApiUtil {

    public final static String TOKEN = "APPCODE ade97caaf6e24a5e88bb95d455677c90";

    /**
     * 股票列表
     */
    public static JSONObject list() {
        JSONObject result = execute("https://thstock.market.alicloudapi.com/list", Collections.EMPTY_MAP);
        return result;
    }

    /**
     * 股票信息
     */
    public static JSONObject real(String symbol) {
        JSONObject result = execute("https://thstock.market.alicloudapi.com/real", ImmutableMap.of("symbol", symbol));
        return result;
    }

    public static JSONObject trend(String symbol) {
        JSONObject result = execute("https://thstock.market.alicloudapi.com/trend", ImmutableMap.of("symbol", symbol));
        return result;
    }

    public static JSONObject kline(String symbol, String type, String from, String to) {
        Map<String, Object> params = ImmutableMap.of(
                "symbol", symbol,
                "type", type,
                "from", from,
                "to", to
        );
        JSONObject result = execute("https://thstock.market.alicloudapi.com/kline", params);
        return result;
    }


    private static JSONObject execute(String api, Map<String, Object> params) {
        String resp = HttpUtil.createGet(api)
                .header(Header.AUTHORIZATION, TOKEN)
                .form(params)
                .execute()
                .body();
        System.out.println(resp);
        JSONObject result = JSONUtil.parseObj(resp);
        return result;
    }

    public static void main(String[] args) {
        //list();
        //real("NYT");
        //trend("NYT");
       //long from = DateUtil.parse("2023-03-01").getTime() / 1000;
       //long to   = DateUtil.parse("2023-03-14").getTime() / 1000;
       //System.out.println(from);
       //System.out.println(to);
       //kline("AJ", "D", String.valueOf(from), String.valueOf(to));
    }


}
